package org.example.controller;

import org.example.annotation.LoginCheck;
import org.example.domain.User;
import org.example.kafka.Event;
import org.example.kafka.MessageConsumer;
import org.example.kafka.MessageProducer;
import org.example.service.imp.FollowServiceImp;
import org.example.service.imp.UserServiceImp;
import org.example.util.CommonParam;
import org.example.util.HutoolUtil;
import org.example.util.UserThreadLocalUtil;
import org.example.vo.FansVO;
import org.example.vo.FollowVO;
import org.example.vo.LimitPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Stream;

@Controller
@RequestMapping("/follow/")
public class FollowController {
    @Autowired
    private UserServiceImp userServiceImp;
    @Autowired
    private FollowServiceImp followServiceImp;
    @Autowired
    private UserThreadLocalUtil userThreadLocalUtil;
    @Autowired
    private HutoolUtil hutoolUtil;
    @Autowired
    private MessageProducer messageProducer;
    @LoginCheck
    @ResponseBody
    @PostMapping("followMe")
    public Integer followMe(int entityType,int entityId){
        int userid = userThreadLocalUtil.get().getId();
        followServiceImp.follow(userid,entityType,entityId);
        //kafka关注消息处理  取关就不处理了
        Event event = new Event();
        //关注自能关注别人  所以entityId就是别人
        event.setTopic(CommonParam.KAFKA_TOPIC_FOLLOW).setUserId(userid).setEntityType(entityType).setEntityId(entityId).setEntityUserId(entityId);
        messageProducer.send(event);
        return 1;
    }
    @LoginCheck
    @ResponseBody
    @PostMapping("unFollowMe")
    public Integer unFollowMe(int entityType,int entityId){
        int userid = userThreadLocalUtil.get().getId();
        followServiceImp.unFollow(userid,entityType,entityId);
        return 1;
    }

    /**
     * 这个方法像坨屎一样   我也不想动了
     * @param currPageUserId
     * @param page
     * @param model
     * @return
     */
    //可以展示别人的关注  但是不能取关或者关注
    @GetMapping("followee/{currPageUserId}/{page}")
    public String followeePage(@PathVariable("currPageUserId") Integer currPageUserId,@PathVariable(value = "page",required = false) Integer page, Model model){
        if (currPageUserId == null || currPageUserId<=0){
            //不知道获取那个用户的关注列表
            return "";
        }
        if (page == null){
            page = 1;
        }
       //展示用户关注的
        int loginUserId = userThreadLocalUtil.get().getId();
        //需要分页
        if (loginUserId == currPageUserId){
            //查询登录用户的关注列表
            FollowVO followVO = followServiceImp.getFollowOrFans(3, loginUserId, CommonParam.FOLLOW_STATUS);

            LimitPageVO limitPageVO = hutoolUtil.limitPage(page, 1, followVO.getFollowUserIds().size());
            limitPageVO.setPath("/follow/followee/"+loginUserId+'/');
            int mysqlLimit = 1;
            int mysqlPage = (limitPageVO.getCurrent()-1)*mysqlLimit;
            //页面需要展示的分页用户
            List<User> users = userServiceImp.selectUsers(followVO.getFollowUserIds(),mysqlPage,mysqlLimit);
            //时间需要做分页
            List<Date> newFollowUserTime = new ArrayList<>();
            Map<Integer, Date> followUserTime = followVO.getFollowUserTime();
            for (int i = 0;i<users.size();i++){
                int id = users.get(0).getId();
                if (followUserTime.containsKey(id)){
                    newFollowUserTime.add(followUserTime.get(id));
                }
            }
            model.addAttribute("followUsers",users);
            model.addAttribute("followUsersTime",newFollowUserTime);
            model.addAttribute("currentUser",userThreadLocalUtil.get());
            model.addAttribute("currPageUserIsOther",false);
            model.addAttribute("limitParam",limitPageVO);
        }else {
            //别人的关注列表
            FollowVO followVO = followServiceImp.getFollowOrFans(3, currPageUserId, CommonParam.FOLLOW_STATUS);

            LimitPageVO limitPageVO = hutoolUtil.limitPage(page, 1,followVO.getFollowUserIds().size());
            limitPageVO.setPath("/follow/followee/"+currPageUserId+'/');
            model.addAttribute("limitParam",limitPageVO);

            int mysqlLimit = 1;
            int mysqlPage = (limitPageVO.getCurrent()-1)*mysqlLimit;
            List<User> users = userServiceImp.selectUsers(followVO.getFollowUserIds(),mysqlPage,mysqlLimit);
            //时间需要做分页
            List<Date> newFollowUserTime = new ArrayList<>();
            Map<Integer, Date> followUserTime = followVO.getFollowUserTime();
            for (int i = 0;i<users.size();i++){
                int id = users.get(0).getId();
                if (followUserTime.containsKey(id)){
                    newFollowUserTime.add(followUserTime.get(id));
                }
            }
            model.addAttribute("followUsers",users);
            model.addAttribute("followUsersTime",newFollowUserTime);
            model.addAttribute("currPageUserIsOther",true);
            User user = userServiceImp.searchById(currPageUserId);
            model.addAttribute("currentUser",user);
        }

        return "site/followee";
    }

    //可以展示别人的关注  但是不能取关或者关注
    @GetMapping("follower/{currPageUserId}/{page}")
    public String followerPage(@PathVariable("currPageUserId") Integer currPageUserId,@PathVariable(value = "page",required = false) Integer page,Model model){
        //其实这个跟上面那个功能很想  但是上面那个写的像屎山一样  我不敢用了 再写一下 看看
        //展示用户的粉丝 需要分页
        //如果展示页的用户不是自己  关注按钮不能展示 不能操作 只能看
        boolean currPageIsLoginSelf = false;
        boolean followButtonShowStatus = false;

        User loginUser = userThreadLocalUtil.get();

        if (loginUser == null){
            //没有登录  访问所有页面都是不能操作的  因为没有登录过 导致不能判断当前页面是否是自己的  默认false
        }else {
            //登录过  判断当前访问页面的用户是否是登录用户   额如果浏览器没有传currPageUserId 怎么办？
            if (currPageUserId == null) throw new RuntimeException("不知道访问具体那个用户");
            if (loginUser.getId() == currPageUserId){
                //是访问登录用户的页面
                currPageIsLoginSelf = true;
                followButtonShowStatus = true;
            }else {
                //不是访问登录用户的页面

            }
        }
        if (page==null || page<=0){
            page = 1;
        }

        FollowVO followUser = followServiceImp.getFollowOrFans(3, currPageUserId,CommonParam.FANS_STATUS);
        int size = 1;
        LimitPageVO limitPageVO = hutoolUtil.limitPage(page,size, followUser.getFollowUserIds().size());
        limitPageVO.setPath("/follow/follower/"+currPageUserId+'/');
        model.addAttribute("limitParam",limitPageVO);
        int mysqlPage = (limitPageVO.getCurrent()-1)*size;
        //这里好像要有点问题  mybatis 好像不会用我们集合中的顺序进行分页   那对应关系出现问题
        List<User> users = userServiceImp.selectUsers(followUser.getFollowUserIds(), mysqlPage, size);
        Map<Integer, Date> followUserTime = followUser.getFollowUserTime();
        List<FansVO> fansVOList = new ArrayList<>();
        if (users!=null && users.size()>0){
            for (User user : users){
                FansVO fans = new FansVO();
                fans.setFansUser(user);
                fans.setFollowTime(followUserTime.get(user.getId()));
                //当前用户是否关注过粉丝
                boolean follow = followServiceImp.isFollow(currPageUserId, 3, user.getId());
                fans.setOtherFollowMe(follow);
                fansVOList.add(fans);
            }
            model.addAttribute("fans",fansVOList);
        }

        model.addAttribute("currPageUserIsSelf",currPageIsLoginSelf);
        if (currPageIsLoginSelf){
            model.addAttribute("currentUser",userThreadLocalUtil.get());
        }else {
            User user = userServiceImp.searchById(currPageUserId);
            model.addAttribute("currentUser",user);
        }
        return "site/follower";
    }
}
